using AutoMapper;
using BaseData.CommonLib.Models;
using BaseData.DomainLib.AggregatesModel.CustomerAggregate;
using MediatR;
using Swimj.CommonLib.Base;

namespace BaseData.ApplicationLib.Commands
{
    public class UpdateCustomerCommand : CustomerEditRequestDto, IRequest<Result<CustomerInfoResponseDto>>
    {
        public int Id { get; set; }
    }

    public class UpdateCustomerCommandHandler : IRequestHandler<UpdateCustomerCommand, Result<CustomerInfoResponseDto>>
    {

        private readonly ICustomerRepository _customerRepository;
        private readonly IMapper _mapper;

        public UpdateCustomerCommandHandler(ICustomerRepository customerRepository,
        IMapper mapper)
        {
            this._customerRepository = customerRepository;
            this._mapper = mapper;
        }

        public async Task<Result<CustomerInfoResponseDto>> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
        {
            var customer = await _customerRepository.Get(request.Id);
            customer?.Update(request.Name, request.Phone, request.Email, request.Address, request.Description);

            await _customerRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

            return await Result<CustomerInfoResponseDto>.SuccessAsync(_mapper.Map<CustomerInfoResponseDto>(customer));
        }
    }
}